package tw.com.softleader.fire.setting.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import tw.com.softleader.fire.common.dao.GenericDao;
import tw.com.softleader.fire.setting.entity.AddrZipEntity;
import tw.com.softleader.fire.setting.entity.InsuredItemEntity;

@Repository
public class AddrZipDao extends GenericDao<AddrZipEntity> {
	
	@Autowired
	private SessionFactory sessionFactory;
	
	private static final String FIND_BY_CODE = "select * from set_addr_zip where addr_code like :code";

	public List<AddrZipEntity> findByCode(String code){
		Session session = sessionFactory.getCurrentSession();

		StringBuffer sb = new StringBuffer();
		
		
		if(  code != null) {
			sb.append(code);
		}
		
		sb.append("%");

		Query query = session.createSQLQuery(FIND_BY_CODE).addEntity(
				AddrZipEntity.class);

		List<AddrZipEntity> models = query.setString("code",
				sb.toString()).list();

		return models;
	}
	
/*	public static void main(String[] args) {
		AddrZipDao dao = new AddrZipDao();
		System.out.println(dao.findByCode("100"));
	}*/
	
/*	@SuppressWarnings("unchecked")
	public List<AddrZipEntity> findByCode(AddrZipEntity entity){
		
		Session session = sessionFactory.getCurrentSession();
		StringBuffer sql = new StringBuffer();
		
		sql.append("select * from set_addr_zip where addr_code like :code and addr_name like :name");
		Query query = session.createSQLQuery(
				sql.toString()).addEntity(AddrZipEntity.class);
		
		if( StringUtils.isEmpty(entity.getAddrCode()) ){
			entity.setAddrCode("%");
		}
		if( StringUtils.isEmpty(entity.getAddrName()) ){
			entity.setAddrName("%");
		}
		
		List<AddrZipEntity> models = query.
				//setString("code", StringUtils.isEmpty(entity.getAddrCode().toString()) ? "%" : entity.getAddrCode()).
				//setString("name", StringUtils.isEmpty(entity.getAddrName().toString()) ? "%" : entity.getAddrName()).
				setString("code", entity.getAddrCode()).
				setString("name", entity.getAddrName()).
				list();
		
		return models;
	}*/
	
	
}
